//
//  YCCarServiceViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/6.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  班车服务

#import "YCCarServiceViewController.h"
#import "YCCarServiceDetailViewController.h"
#import "YCCarServiceModel.h"


@interface YCCarServiceViewController ()<UITableViewDelegate,UITableViewDataSource>

/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 数据  */
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation YCCarServiceViewController

//- (NSArray *)dataArray
//{
//    if (!_dataArray) {
//        _dataArray = @[
//                       @"昆明市区A线",
//                       @"昆明市区B线",
//                       @"昆明市区C线",
//                       @"西市区A线",
//                       @"西市区B线",
//                       @"西市区C线",
//                       @"西市区D线",
//                       @"滇池A线",
//                       @"滇池B线",
//                       @"北市区A线",
//                       @"北市区B线",
//                       @"北市区C线",
//                       ];
//    }
//    return _dataArray;
//}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.yc_width, self.view.yc_height-64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self yc_addBackBarButtonItem];
    self.navigationItem.title = @"班车服务";
    [self.view addSubview:self.tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.rowHeight = 48;
    YCWeakSelf
    self.tableView.mj_header = [YCRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf loadDataByAfnOrigin];
    }];
    [self loadDataByAfnOrigin];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - 数据处理
// 不用自己封装的,这个接口特别.返回值没有result msg code等
- (void)loadDataByAfnOrigin
{
    NSDictionary *params = @{//暂时不用传参
                             //                             @"uid":@"1",
                             //                             @"token":accessToken_Test,
                             };
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    [mgr POST:[YCAPIBus appendBaseUrl] parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.dataArray = [YCCarServiceModel yc_objectArrayWithKeyValuesArray:responseObject];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        YCLog(@"%@",error.localizedDescription);
        [self.tableView.mj_header endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"carServiceCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"carServiceCell"];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.textColor = [UIColor yc_colorWithUInt:0x333333];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    YCCarServiceModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = model.title;
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YCLogFunc
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YCCarServiceModel *model = self.dataArray[indexPath.row];
    YCCarServiceDetailViewController *carDetailVc = [[YCCarServiceDetailViewController alloc] initWithCarServiceModel:model];
    [self.navigationController pushViewController:carDetailVc animated:YES];
}




@end
